package com.yxplearn.mybatisplus.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.yxplearn.mybatisplus.manager.GameExplorer;

@Component
public class LoginFilter implements Filter {
	private static final Logger log = LoggerFactory.getLogger(LoginFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Map<Object, Object> map = new LinkedHashMap<>(10);
        map.put("filterName", filterConfig.getFilterName());
        log.info("filter config indclude :{}", map);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String uri = request.getRequestURI();

        Map<Object, Object> map = new HashMap<>(10);
        map.put("url", request.getRequestURL());
        map.put("uri", uri);
        map.put("requestType", request.getMethod());
//        log.info("{}", map);
        
        GameExplorer.putLocalPlayerId(1);

        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        log.info("filter destroyed ...");
    }
}
